extern crate failure;
extern crate ibm_mq_rs;
use failure::Error;
use ibm_mq_rs::*;

#[test]
fn put() -> Result<(), Error>{
    let qm = MQueueManager::connect("QM1", "DEV.APP.SVRCONN", "localhost(1414)", "app", None)?;
    let mut queue = MQueue::builder()
        .queue_mgr(&qm)
        .name("DEV.QUEUE.1")
        .build_queue()?;
    let open_options = MQOpenOpts::builder()
        .with_option(mqi::MQOO_OUTPUT).build();
    queue.open(open_options)?;
    let put_msg_opts = MQPutMsgOpts::builder()
        .with_option(mqi::MQPMO_NO_SYNCPOINT).build();
    for i in 1..10 {
        let msg = MQMessage::from(format!("someputmessage - {}", i));
        queue.put(&msg, &put_msg_opts)?;
    } 
    queue.close();
    qm.disconnect();
    Ok(())
}

#[test]
fn get() -> Result<(), Error> {
    let qm = MQueueManager::connect("QM1", "DEV.APP.SVRCONN", "localhost(1414)", "app", None)?;
    let mut queue = MQueue::builder()
        .queue_mgr(&qm)
        .name("DEV.QUEUE.1")
        .build_queue()?;
    let open_options = MQOpenOpts::builder()
        .with_option(mqi::MQOO_INPUT_SHARED).build();
    queue.open(open_options)?;
    let get_msg_opts = MQGetMsgOpts::builder()
        .wait_interval(1000)
        .build();
    if let Some(msg) = queue.get(&get_msg_opts)? {
        println!("Get received: {}", msg);
    } 
    queue.close();
    qm.disconnect();
    Ok(())
}

#[test]
fn publish() -> Result<(), Error>{
    let qm = MQueueManager::connect("QM1", "DEV.APP.SVRCONN", "localhost(1414)", "app", None)?;
    let open_options = MQOpenOpts::builder()
        .with_option(mqi::MQOO_OUTPUT)
        .with_option(mqi::MQOO_FAIL_IF_QUIESCING).build();
    
    let mut topic = MQTopic::builder()
        .queue_mgr(&qm)
        .name("DEV.BASE.TOPIC")
        .build_topic()?;

    topic.open(open_options)?;
     
    let put_msg_opts = MQPutMsgOpts::builder()
        .with_option(mqi::MQPMO_NO_SYNCPOINT).build();
    
    for i in 1..10 {
        let msg = MQMessage::from(format!("blahblah{}", i).as_ref());
        topic.put(&msg, &put_msg_opts)?;
    }
    topic.close();
    qm.disconnect();
    Ok(())
}

#[test]
fn subscribe() -> Result<(), Error>{
    let qm = MQueueManager::connect("QM1", "DEV.APP.SVRCONN", "localhost(1414)", "app", None)?;
    let subscription = MQTopic::builder()
        .queue_mgr(&qm)
        .name("DEV.BASE.TOPIC")
        .build_topic()?;
    let sub_desc = MQSubDesc::builder()
        .with_option(mqi::MQSO_CREATE)
        .with_option(mqi::MQSO_NON_DURABLE)
        .with_option(mqi::MQSO_FAIL_IF_QUIESCING)
        .with_option(mqi::MQSO_MANAGED)
        .topic_name("DEV.BASE.TOPIC").build();
    subscription.subscribe(&sub_desc)?;

    let get_msg_opts = MQGetMsgOpts::builder()
        .with_option(mqi::MQGMO_ACCEPT_TRUNCATED_MSG)
        .with_option(mqi::MQGMO_NO_SYNCPOINT)
        .with_option(mqi::MQGMO_CONVERT)
        .wait_interval(1000)
        .build();
    // loop {
        match  subscription.get(&get_msg_opts) {
            Ok(Some(x)) => println!("Received a message {}", x),
            Ok(None)    => println!("No message available"),
            Err(e)      => println!("GET error: {}", e),
        }
    // }
    subscription.close();
    qm.disconnect();
    Ok(())
}
